# -*- coding: utf-8 -*-
'''方便测试，从这里启动所有的爬虫。如果有需要的话，可以单独启动每一项。
'''
from __future__ import absolute_import

import logging
import os
import sys
from multiprocessing import Process

import multiprocessing_logging

# add up one level dir into sys path
sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(
    os.path.dirname(os.path.dirname(__file__))))))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "base.settings")

from common.lottery.cyclical.cq_ssc.crawler import main as cq_ssc
from common.lottery.cyclical.cq_lf.crawler import main as cq_lf
from common.lottery.cyclical.tj_ssc.crawler import main as tj_ssc
from common.lottery.cyclical.xj_ssc.crawler import main as xj_ssc
from common.lottery.cyclical.js_ks.crawler import main as js_ks
from common.lottery.cyclical.gx_ks.crawler import main as gx_ks
from common.lottery.cyclical.sd_11x5.crawler import main as sd_11x5
from common.lottery.cyclical.gd_11x5.crawler import main as gd_11x5
from common.lottery.cyclical.jx_11x5.crawler import main as jx_11x5
from common.lottery.cyclical.sh_11x5.crawler import main as sh_11x5
from common.lottery.cyclical.bj_pk10.crawler import main as bj_pk10
from common.lottery.cyclical.tc_pls.crawler import main as tc_pls
from common.lottery.cyclical.fc3d.crawler import main as fc3d
from common.lottery.cyclical.ff_ssc.crawler import main as ff_ssc
from common.lottery.cyclical.ff_11x5.crawler import main as ff_11x5
from common.lottery.cyclical.ff_ks.crawler import main as ff_ks
from common.lottery.cyclical.ff_pk10.crawler import main as ff_pk10

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)s %(message)s',
                    filename='/var/log/bigbang/crawler.log',
                    filemode='w')

multiprocessing_logging.install_mp_handler()

if __name__ == '__main__':
    targets = [
        cq_ssc,
        cq_lf,
        tj_ssc,
        xj_ssc,
        js_ks,
        gx_ks,
        sd_11x5,
        gd_11x5,
        jx_11x5,
        sh_11x5,
        bj_pk10,
        tc_pls,
        fc3d,
        ff_ssc,
        ff_ks,
        ff_11x5,
        ff_pk10,
    ]
    args = sys.argv
    date = sys.argv[1] if len(sys.argv) > 1 else None
    p_list = []
    for target in targets:
        p = Process(target=target, args=(date,))
        p_list.append(p)
        p.start()

    for p in p_list:
        p.join()
